local CheckWidgetType = UI.CheckWidgetType
local CheckArgType = UI.CheckArgType
local MakeAccessor = UI.MakeAccessor
local MakeTypeSetter = UI.MakeTypeSetter

--[[ FontString @FIXME ]]--
local FontString = UI.NewWidgetType("FontString", {"LayeredRegion", "Font"})
FontString.parameters = {
	-- text = "",
	textheight = false,
}

function FontString:Initialize(o)
	local t = UI.params[o]
	assert(t)
end


function FontString.prototype:SetText(text)
	CheckWidgetType("FontString:SetText", self, "FontString")
	CheckArgType("FontString:SetText", 2, "text", {"string", "number", "function", "nil"}, text)
	local t = UI.params[self]
	if type(text) == "function" then
		local s, v = pcall(text)
		if not s then
			text = v
		else
			text = nil
		end
	end
	t.text = text and tostring(text) or text
	if t.name == "ScriptErrors_Message" then
		error("EE ScriptErrors_Message:SetText ".. text)
	end
end

function FontString.prototype:SetFormattedText(s, ...)
	CheckWidgetType("FontString:SetFormattedText", self, "FontString")
	CheckArgType("FontString:SetFormattedText", 2, "text", "string", s)
	self:SetText(s:format(...))
end

MakeAccessor("FontString", "GetText", "text")
MakeTypeSetter("FontString", "SetTextHeight", "textheight", "number")

function FontString.prototype:SetNonSpaceWrap(flag)
	CheckWidgetType("FontString:SetNonSpaceWrap", self, "FontString")
	local t = UI.params[self]
	t.nonspacewrap = flag
end

function FontString.prototype:GetStringWidth()
	CheckWidgetType("FontString:GetStringWidth", self, "FontString")
	local t = UI.params[self]
	local text = t.text
	if not text then return 0 end
	return text:len() * t.height
end
